//
//  SecondStepController.m
//  GroceryStoryboards
//
//  Created by jose on 18/03/13.
//  Copyright (c) 2013 Jose Luis Lopez. All rights reserved.
//

#import "SecondStepController.h"
#import "Unities.h"

@interface SecondStepController ()

@end

@implementation SecondStepController

@synthesize secondNext, unityTableView, unityButtonTableView, unitiesIndex, unityString, unityText;

- (void)viewDidLoad
{
    [super viewDidLoad];

}

- (IBAction)nextStep:(UIButton *)sender {
    
    if ([self.parentViewController respondsToSelector:@selector(nextStep)])
        [self.parentViewController performSelector:@selector(nextStep)];
}

- (IBAction)previousStep:(UIButton *)sender {
    
    if ([self.parentViewController respondsToSelector:@selector(prevStep)])
        [self.parentViewController performSelector:@selector(prevStep)];
}

- (IBAction)animateTableView:(UIButton *)sender {

    [self.unityButtonTableView setHidden:YES];
    
    [self.unityTableView scrollToRowAtIndexPath:[NSIndexPath indexPathForItem:0 inSection:0] atScrollPosition:UITableViewScrollPositionTop animated:NO];
    
    [UIView animateWithDuration:0.3 delay:0 options:UIViewAnimationOptionCurveEaseIn animations:^(void) {
        
        
            NSInteger tableViewHeight;
            if ([[UIScreen mainScreen] bounds].size.height > 480)
                tableViewHeight = 298 + 44;
            else
                tableViewHeight = 210 + 44;
            
            [self.unityTableView setFrame:CGRectMake(self.unityTableView.frame.origin.x, self.unityTableView.frame.origin.y, self.unityTableView.frame.size.width, tableViewHeight)];

    } completion:nil];
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    [self.unityButtonTableView setFrame:CGRectMake(0, self.unityButtonTableView.frame.origin.y, self.unityButtonTableView.frame.size.width, self.unityButtonTableView.frame.size.height)];
    [self.unityButtonTableView setHidden:NO];
    
    self.unityString = [tableView cellForRowAtIndexPath:indexPath].textLabel.text;
    self.unitiesIndex = indexPath.row;
    
    [UIView animateWithDuration:0.3 animations:^(void) {
        
        [self.unityTableView setFrame:CGRectMake(self.unityTableView.frame.origin.x, self.unityTableView.frame.origin.y, self.unityTableView.frame.size.width, 44)];
        
    } completion:^(BOOL res) {
        
        [tableView scrollToNearestSelectedRowAtScrollPosition:UITableViewScrollPositionTop animated:YES];
        
        [unityText setText:[[Unities singularUnities] objectAtIndex:indexPath.row]];
        
        if ([self.parentViewController respondsToSelector:@selector(unitySelected:)])
            [self.parentViewController performSelector:@selector(unitySelected:) withObject:[NSNumber numberWithInteger:indexPath.row]];
        
        [tableView deselectRowAtIndexPath:indexPath animated:YES];
        
        if ([[[Unities singularUnities] objectAtIndex:indexPath.row] length] > 0)
            if ([self.parentViewController respondsToSelector:@selector(nextStep)])
                [self.parentViewController performSelector:@selector(nextStep)];
        
    }];
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return [[Unities singularUnities] count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    UITableViewCell *cell = [[UITableViewCell alloc] init];
    
    cell.textLabel.text = [[Unities singularUnities] objectAtIndex:indexPath.row];
    cell.textLabel.textColor = [UIColor darkGrayColor];
    cell.textLabel.font = [UIFont fontWithName:@"Avenir-Light" size:16.0f];
    
    [cell.textLabel setTextAlignment:NSTextAlignmentCenter];
    
    return cell;
}

#pragma mark Scroll Control

- (void)hideControls {
 
    [UIView animateWithDuration:0.3 animations:^(void) {
        
        [self.unityTableView setFrame:CGRectMake(self.unityTableView.frame.origin.x, self.unityTableView.frame.origin.y, self.unityTableView.frame.size.width, 44)];
        
    } completion:^(BOOL res) {
        
    }];
}

- (void)showControls {

    [self animateTableView:nil];
}

@end
